package com.xyzwps.website.modules.user.service;

import com.xyzwps.website.modules.sms.model.VcodeScene;
import com.xyzwps.website.modules.sms.service.VcodeService;
import jakarta.inject.Singleton;

@Singleton
public class SimpleAuthenticationService {

    private final VcodeService vcodeService;

    private final UserService userService;

    public SimpleAuthenticationService(VcodeService vcodeService, UserService userService) {
        this.vcodeService = vcodeService;
        this.userService = userService;
    }

    private void phoneShouldNotBeRegistered(String phone) {
        if (userService.getUserByPhone(phone).isPresent()) {
            throw new IllegalArgumentException("phone already registered");
        }
    }

    public void sendRegisterVcode(String phone) {
        this.phoneShouldNotBeRegistered(phone);
        vcodeService.sendVcode(phone, VcodeScene.REGISTER);
    }

    public void register(String phone, String vcode) {
        this.phoneShouldNotBeRegistered(phone);
        vcodeService.verifyVcode(phone, VcodeScene.REGISTER, vcode);
        userService.createUserByPhone(phone);
    }

    public void login(String username, String password) {
        System.out.println("login");
    }
}
